using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class Menu : BaseItem
    {
        private Menu() : base() { }
        private Menu(Hashtable table) : base(table) { }
        private Menu(Hashtable table, List<Menu> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new MenuDal();
        }
        
        public static Menu CreateInstance()
        {
            return new Menu();
        }

        private static Menu Get(Hashtable table)
        {
            if (table != null)
                return new Menu(table);
            else
                return null;
        }

        private static List<Menu> Get(Hashtable[] tables)
        {
            if (tables == null)
            {
                return null;
            }
            else
            {
                List<Menu> tmp = new List<Menu>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new Menu(table, tmp));
                }
                return tmp;
            }
        }

        public static List<Menu> GetAllBySiteId(int siteId)
        {
            MenuDal dal = new MenuDal();
            return Get(dal.GetAllBySiteId(siteId));
        }

        
        public static Menu GetById(int id, int siteId)
        {
            MenuDal dal = new MenuDal();
            return Get(dal.GetById(id, siteId));
        }

        public static Menu GetByNameAndSiteId(string name, int siteId)
        {
            MenuDal dal = new MenuDal();
            return Get(dal.GetByNameAndSiteId(name, siteId));
        }
        
        protected override void Load(Hashtable table)
        {
            Id = (int)table[MenuDal.Fields.Id];
            SiteId  = (int)table[MenuDal.Fields.SiteId];
            Name = (string)table[MenuDal.Fields.Name];
            ParentPath = (string)table[MenuDal.Fields.ParentPath];
            IncludeParent = (bool)table[MenuDal.Fields.IncludeParent];
            DefaultForAllPosts = (bool)table[MenuDal.Fields.DefaultForAllPosts];
            Notes = (string)table[MenuDal.Fields.Notes];
        }

        public int Id { get; private set; }
        public int SiteId { get; private set; }
        public string Name { get; set; }
        public string ParentPath { get; set; }
        public bool IncludeParent { get; set; }
        public bool DefaultForAllPosts { get; set; }
        public string Notes { get; set; }

        public void SetSiteId(int siteId)
        {
            if (IsNewRecord)
                SiteId = siteId;
            else
                throw new ApplicationException("Can only set SiteId when creating a menu");
        }

        public new void Update()
        {
            base.Update();
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
             int idOut = Id;
             ((MenuDal)dal).Update(ref idOut, SiteId, Name, ParentPath, IncludeParent, DefaultForAllPosts, Notes, ref concurencyId);
             Id = idOut;
             return idOut;
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
             ((MenuDal)dal).Delete(Id, concurencyId);
        }

        public string ConvertParentPath(string currentUrl)
        {
            StringBuilder tmp = new StringBuilder();
            if (ParentPath.StartsWith("~/"))
            {
                tmp.Append(ParentPath);
            }
            else if (ParentPath.StartsWith("../"))
            {
                string tmpParentPath = ParentPath;
                string tmpUrl = currentUrl;
                while (tmpParentPath.StartsWith("../"))
                {
                    if (tmpUrl.IndexOf("/") > 0)
                    {
                        tmpUrl = tmpUrl.Remove(tmpUrl.LastIndexOf("/"));
                    }
                    tmpParentPath = tmpParentPath.Substring(3);
                }
                tmp.Append(tmpUrl);
                tmp.Append("/");
                tmp.Append(tmpParentPath);
            }
            else
            {
                tmp.Append(currentUrl);
                tmp.Append(ParentPath);
            }
            return tmp.ToString();
        }

    }
}
